package com.xxx.video.commen.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.core.content.FileProvider;


import com.xxx.video.commen.configs.Constant;

import java.io.File;

/**
 *
 */
public class InstallOrLaunchUtils {
    private static InstallOrLaunchUtils mInstallOrLaunchUtils = null;

    private InstallOrLaunchUtils() {
    }

    public static InstallOrLaunchUtils getInstance() {
        if (mInstallOrLaunchUtils == null) {
            synchronized (InstallOrLaunchUtils.class) {
                if (mInstallOrLaunchUtils == null) {
                    mInstallOrLaunchUtils = new InstallOrLaunchUtils();
                }
            }
        }
        return mInstallOrLaunchUtils;
    }

    /**
     * open or insttall app
     *
     * @param context     context
     * @param pkgName     pkg name
     * @param versionCode app versionCode
     * @return state 1 start
     */
    public int launchOrInstallApp(Context context, String pkgName, String url, long versionCode) {
        if (!TextUtils.isEmpty(pkgName)) {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkgName);
            if (intent == null) {
                installApk(context, new File(Constant.DOWNLOAD_VIDEO_PATH + DownloadUtils.getInstance().getFileName(url)));
            } else {
                try {
                    PackageInfo packageInfo = context.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_ACTIVITIES);
                    if (packageInfo.versionCode >= versionCode) {
                        /*
                         * launch
                         */
                        context.startActivity(intent);
                        return 1;
                    } else {
                        /*
                         * install
                         */
                        installApk(context, new File(Constant.DOWNLOAD_VIDEO_PATH + DownloadUtils.getInstance().getFileName(url)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            Toast.makeText(context, "package name can not empty！", Toast.LENGTH_SHORT).show();
        }
        return 0;
    }

    /**
     * 1 request permission：WRITE_EXTERNAL_STORAGE and REQUEST_INSTALL_PACKAGES ；2 xml FileProvider
     */
    public void installApk(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri;
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        Log.i("bqt", "【Uri】" + uri);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(intent);
    }


}
